package by.hatton.engine.service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import by.hatton.engine.bo.BaseRequestDataWrapper;
import by.hatton.engine.processor.AbstractProcessorFactory;
import by.hatton.engine.processor.BaseProcessor;

import android.app.Service;
import android.content.Intent;

public abstract class BaseProcessorService extends Service{

	private static final int NUMBER_THREADS = 5;
	private ExecutorService mExecutorService; 
	private AbstractProcessorFactory mProcessorFactory;

	@Override
	public void onCreate() {
		mExecutorService = Executors.newFixedThreadPool(NUMBER_THREADS);
		super.onCreate();
		setBasicProcessorFactory();
	}
	
	public abstract void setBasicProcessorFactory();

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		BaseRequestDataWrapper baseRequestDataWrapper = new BaseRequestDataWrapper(intent.getExtras().getBundle("Request data"));
		final BaseProcessor processor = mProcessorFactory.getProcessor(baseRequestDataWrapper);
		mExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				processor.start();
			}
		});
		return START_STICKY;
	}
	
	public AbstractProcessorFactory getProcessorFactory() {
		return mProcessorFactory;
	}

	public void setProcessorFactory(AbstractProcessorFactory factory) {
		this.mProcessorFactory = factory;
	}
	
}
